package com.detrum;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.detrum.adapter.ShareListAdatper;
import com.detrum.base.BaseActivity;
import com.detrum.base.R;
import com.detrum.bean.ShareBean;
import com.detrum.costomview.BaseTitle;
import com.detrum.costomview.BaseTitleCall;
import com.detrum.sqlite.SqliteShareOperate;
import com.detrum.utils.LogUtils;

import java.io.ByteArrayOutputStream;
import java.util.List;

public class ShareSettingActivity extends BaseActivity{
    private BaseTitle share_title;
    private ListView shareList;
    private ShareListAdatper shareListAdatper;
    private List<ShareBean> beanList;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==21){
                initializeShare();
            }
        }
    };

    private PackageManager pm;
    private List<ResolveInfo> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_setting);
        initView();
        pm = getPackageManager();
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        list = pm.queryIntentActivities(intent,0);
        LogUtils.i("ShareSettingActivity list size = "+list.size());
        new Thread(new Runnable() {
            @Override
            public void run() {
                LogUtils.i("ShareSettingActivity Thread is start ");
                getShareList();
            }
        }).start();
    }

    private void initView() {
        share_title = findViewById(R.id.share_title);
        shareList = findViewById(R.id.shareList);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void getShareList(){
        for (int i = 0; i < list.size(); i++) {
            ShareBean bean = new ShareBean();
            String name = list.get(i).loadLabel(pm).toString();
            bean.setName(name);
            ActivityInfo activityInfo = list.get(i).activityInfo;
            String activityName = activityInfo.name;
            String packageName = activityInfo.applicationInfo.packageName;
            bean.setActivityName(activityName);
            bean.setPackageName(packageName);
            bean.setSelect(1);
            long insert = SqliteShareOperate.getInstence(ShareSettingActivity.this).Insert(bean);
            LogUtils.i("share insert = " + insert);
        }
        beanList = SqliteShareOperate.getInstence(ShareSettingActivity.this).SelectAll();
        for (int i = 0; i < beanList.size(); i++) {
            for (int j = 0; j < list.size(); j++) {
                if(beanList.get(i).getName().equals(list.get(j).loadLabel(pm).toString())){
                    beanList.get(i).setIcon(list.get(j).loadIcon(pm));
                    break;
                }
            }
        }
        LogUtils.i("share beanList = " + beanList.size());
        handler.sendEmptyMessage(21);
    }

    private void initializeShare(){
        shareListAdatper = new ShareListAdatper(ShareSettingActivity.this,beanList);
        shareList.setAdapter(shareListAdatper);
        shareListAdatper.setShareAdapterCall(new ShareListAdatper.ShareAdapterCall() {
            @Override
            public void isClick(int position,boolean isCheck) {
                String name = beanList.get(position).getName();
                int updateSelect;
                if(isCheck){
                    updateSelect = SqliteShareOperate.getInstence(ShareSettingActivity.this).UpdateSelect(name,1);
                }else{
                    updateSelect = SqliteShareOperate.getInstence(ShareSettingActivity.this).UpdateSelect(name,2);
                }
                LogUtils.e("ShareSettingActivity updateSelect = "+name+"==="+isCheck+"==="+updateSelect);
            }
        });
    }


}
